/*
 * 如果只有一个圆的情况下,请设置已完成的圆  默认的圆的颜色不要设置
 */
import 'dart:math';
import 'dart:ui' as ui;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

/// 时间回调
// typedef TacitCountDownPainterCallBack = void Function(int count);

class TacitCountDownPainter extends CustomPainter {
  final Animation<double>? factor;
  final int countDownSum;
  final double width;
  final double borderWidth;
  final double fontSize;

  // /// 时间回调
  // final TacitCountDownPainterCallBack? painterCallBack;
  /// 是否是顺时针（顺时针填充，逆时针消失
  final bool clockwise;

  /// 内部文字颜色
  final Color? textColor;

  /// 环颜色
  final Color? ringColor;

  /// 环背景颜色
  final Color? ringBgColor;

  TacitCountDownPainter({
    this.width = 30,
    this.borderWidth = 4.2,
    this.countDownSum = 10,
    this.fontSize = 18,
    this.factor,
    this.ringColor,
    this.ringBgColor,
    this.textColor,
    this.clockwise = false,
    // this.painterCallBack,
  }) : super(repaint: factor);

  @override
  void paint(Canvas canvas, Size size) {
    drawScaleBackground(canvas, size);
    drawText(canvas, size);
  }

  //刻度背景
  drawScaleBackground(Canvas canvas, Size size) {
    Offset center = Offset(size.width / 2, size.height / 2); //  坐标中心
    double radius =
        min(size.width / 2, size.height / 2) - borderWidth / 2; //  半径
    //创建画笔
    Paint paint = Paint()
      ..style = PaintingStyle.stroke
      ..strokeWidth = borderWidth;

    /// 画环背景
    paint.color =
        ringBgColor ?? Color(0xFFFF99A0).withAlpha((255 * 0.5).toInt());
    double start = 0;
    double arcAngle = 2 * pi;
    canvas.drawArc(
      Rect.fromCircle(center: center, radius: radius),
      start, //  -pi / 2,从正上方开始  pi / 2,从下方开始
      arcAngle,
      false,
      paint,
    );
    paint.color = ringColor ?? Color(0xFFFF99A0);
    start = -pi / 2;
    // this.clockwise,

    arcAngle = 2 * pi * (clockwise ? 1 - factor!.value : factor!.value);
    canvas.drawArc(
      Rect.fromCircle(center: center, radius: radius),
      start, //  -pi / 2,从正上方开始  pi / 2,从下方开始
      arcAngle,
      false,
      paint,
    );
  }

  //文字
  drawText(Canvas canvas, Size size) {
    ui.ParagraphBuilder paragraphBuilder = ui.ParagraphBuilder(
        ui.ParagraphStyle(
            textAlign: TextAlign.center,
            fontSize: fontSize,
            fontWeight: FontWeight.w500));
    paragraphBuilder.pushStyle(ui.TextStyle(
      color: textColor ?? Colors.white,
      fontSize: 18,
      fontWeight: FontWeight.w500,
    ));
    paragraphBuilder
        .addText('${(countDownSum - factor!.value * countDownSum).toInt()}s');
    ui.ParagraphConstraints paragraphConstraints =
        ui.ParagraphConstraints(width: size.width);
    ui.Paragraph paragraph = paragraphBuilder.build()
      ..layout(paragraphConstraints);

    // 获取段落的宽度和高度
    final paragraphWidth = paragraph.width;
    final paragraphHeight = paragraph.height;

    // 计算绘制位置
    final dx = (size.width - paragraphWidth) / 2;
    final dy = (size.height - paragraphHeight) / 2;

    // 绘制段落
    canvas.drawParagraph(paragraph, Offset(dx, dy));
  }

  @override
  bool shouldRepaint(TacitCountDownPainter oldDelegate) => true;
}
